/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UltimatumStrategyComponent.java
 *
 * Created on 15/Dez/2011, 18:19:16
 */

package intopo.game.impl.ultimatum;

import intopo.game.gui.AbstractStrategyEditorComponent;
import intopo.game.gui.ParsingException;
import java.awt.Component;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/**
 *
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 15/Dez/2011
 */
public class UltimatumStrategyComponent
	extends AbstractStrategyEditorComponent<UltimatumStrategy>
{
	/**
	 * 
	 */
	private int error;
	/**
	 *
	 */
	private AcceptsDivisionTableModel tableModel;
	/**
	 * Creates new form BeanForm */
	public UltimatumStrategyComponent ()
	{
		this.error = 0;
		this.tableModel = new AcceptsDivisionTableModel ();
		initComponents();
		// adjust table width
		for (int i = 0; i < 2; i++) {
			TableColumn column = this.acceptsDivisionTable.getColumnModel ().getColumn (i);
			TableCellRenderer headerRenderer = this.acceptsDivisionTable.getTableHeader ().getDefaultRenderer ();
			Component comp = headerRenderer.getTableCellRendererComponent
				(this.acceptsDivisionTable, column.getHeaderValue (), true, true, -1, i);
			int width = comp.getPreferredSize ().width;
			column.setMinWidth (width + 2);
			column.setMaxWidth (width + 4);
		}
	}
	/**
	 * 
	 * @return
	 */
	@Override
	public String getConfigFileDescription () {
		throw new UnsupportedOperationException ("Not supported yet.");
	}
	/**
	 *
	 * @return
	 */
	@Override
	public UltimatumStrategy getStrategy ()
	{
		if (this.error == 0) {
			byte divisionProposal = (Byte) this.divisionProposalFormattedTextField.getValue ();
			boolean[] acceptsDivision = this.tableModel.acceptsDivision.clone ();
			return new UltimatumDeterministicStrategy (divisionProposal, acceptsDivision);
		}
		else {
			return null;
		}
	}
	/**
	 * 
	 * @param strategy
	 */
	@Override
	public void setStrategy (UltimatumStrategy strategy)
	{
		if (strategy instanceof UltimatumDeterministicStrategy) {
			UltimatumDeterministicStrategy s = (UltimatumDeterministicStrategy) strategy;
			this.divisionProposalFormattedTextField.setValue (new Integer (s.getDivisionProposal ()));
			this.tableModel = new AcceptsDivisionTableModel (s);
			this.acceptsDivisionTable.setModel (this.tableModel);
		}
		else {
			throw new UnsupportedOperationException ("Ultimatum strategy not supported yet: " + strategy.getClass ().getCanonicalName ());
		}
	}
	/**
	 * 
	 */
	static final class AcceptsDivisionTableModel
		extends javax.swing.table.AbstractTableModel
	{
		/**
		 *
		 */
		boolean[] acceptsDivision = new boolean [UltimatumStrategy.Parameters.cakeSize + 1];
		/**
		 * 
		 */
		AcceptsDivisionTableModel ()
		{
			this.acceptsDivision = new boolean [UltimatumStrategy.Parameters.cakeSize + 1];
			java.util.Arrays.fill (this.acceptsDivision, true);
		}
		/**
		 *
		 * @param strategy
		 */
		AcceptsDivisionTableModel (UltimatumDeterministicStrategy strategy)
		{
			this.acceptsDivision = new boolean [strategy.getCakeSize () + 1];
			for (int i = this.acceptsDivision.length - 1; i >= 0; i--) {
				this.acceptsDivision [i] = strategy.isDivisionAcceptable (i);
			}
		}
		/**
		 *
		 * @param column
		 * @return
		 */
		public String getColumnName (int column)
		{
			switch (column) {
			case 0:
				return "Division";
			case 1:
				return "Accepts";
			}
			throw new Error ("Unreachable in AcceptsDivisionTableModel.getColumnName(int)");
		}
		/**
		 *
		 * @param rowIndex
		 * @param columnIndex
		 * @return
		 */
		public boolean isCellEditable (int rowIndex, int columnIndex)
		{
			switch (columnIndex) {
			case 0:
				return false;
			case 1:
				return true;
			}
			throw new Error ("Unreachable in AcceptsDivisionTableModel.isCellEditable(int,int)");
		}
		/**
		 * 
		 * @param columnIndex
		 * @return
		 */
		public Class<?> getColumnClass (int columnIndex)
		{
			switch (columnIndex) {
			case 0:
				return Integer.class;
			case 1:
				return Boolean.class;
			}
			throw new Error ("Unreachable in AcceptsDivisionTableModel.getColumnClass(int)");
		}
		/**
		 *
		 * @return
		 */
		@Override
		public int getRowCount ()
		{
			return this.acceptsDivision.length;
		}
		/**
		 *
		 * @return
		 */
		@Override
		public int getColumnCount ()
		{
			return 2;
		}
		/**
		 *
		 * @param rowIndex
		 * @param columnIndex
		 * @return
		 */
		@Override
		public Object getValueAt (int rowIndex, int columnIndex)
		{
			switch (columnIndex) {
			case 0:
				return new Integer (rowIndex);
			case 1:
				return new Boolean (this.acceptsDivision [rowIndex]);
			}
			throw new Error ("Unreachable in AcceptsDivisionTableModel.getValueAt(int,int)");
		}
		/**
		 *
		 * @param value
		 * @param rowIndex
		 * @param columnIndex
		 */
		@Override
		public void setValueAt (Object value, int rowIndex, int columnIndex)
		{
			this.acceptsDivision [rowIndex] = (Boolean) value;
		}
	}
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      deterministicPanel = new javax.swing.JPanel();
      jPanel1 = new javax.swing.JPanel();
      jLabel1 = new javax.swing.JLabel();
      divisionProposalFormattedTextField = new javax.swing.JFormattedTextField();
      jScrollPane1 = new javax.swing.JScrollPane();
      acceptsDivisionTable = new javax.swing.JTable();

      deterministicPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("deterministic strategy"));
      deterministicPanel.setLayout(new java.awt.BorderLayout());

      jLabel1.setText("Division proposal: ");
      jPanel1.add(jLabel1);

      divisionProposalFormattedTextField.setColumns(5);
      divisionProposalFormattedTextField.setValue(new Byte ((byte) (UltimatumStrategy.Parameters.cakeSize / 2)));
      divisionProposalFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            divisionProposalFormattedTextFieldPropertyChange(evt);
         }
      });
      jPanel1.add(divisionProposalFormattedTextField);

      deterministicPanel.add(jPanel1, java.awt.BorderLayout.NORTH);

      jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("division acceptance"));
      jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
      jScrollPane1.setPreferredSize(new java.awt.Dimension(375, 75));

      acceptsDivisionTable.setModel(this.tableModel);
      jScrollPane1.setViewportView(acceptsDivisionTable);

      deterministicPanel.add(jScrollPane1, java.awt.BorderLayout.CENTER);

      add(deterministicPanel);
   }// </editor-fold>//GEN-END:initComponents

	 private void divisionProposalFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_divisionProposalFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final byte value = (Byte) this.divisionProposalFormattedTextField.getValue ();
			final boolean ok = value >= 0 && value <= 1;
			if (ok) {
				this.error &= ~1;
			}
			else {
				this.error |= 1;
			}
			this.update (this.divisionProposalFormattedTextField, ok);
		}
	 }//GEN-LAST:event_divisionProposalFormattedTextFieldPropertyChange



   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JTable acceptsDivisionTable;
   private javax.swing.JPanel deterministicPanel;
   private javax.swing.JFormattedTextField divisionProposalFormattedTextField;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JPanel jPanel1;
   private javax.swing.JScrollPane jScrollPane1;
   // End of variables declaration//GEN-END:variables

}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
